{extend name="index/public" /}

{block name="content"}
<form class="layui-form" action="" id="searchBox">
    <div class="layui-inline">
        <div class="layui-input-inline">
            {MyTag:game type='manage' /}
        </div>
    </div>
    <div class="layui-inline">
        <button class="layui-btn my-btn my-btn-sm" type="button" id="add">添加</button>
    </div>
</form>

<table id="tableBox" lay-filter="tableBox"></table>


<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn-green layui-btn-sm" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
</script>
{/block}

{block name="css"}
{/block}

{block name="js"}
<script>
    layui.use('table', function(){
        var table = layui.table
            ,$ = layui.$
            ,form = layui.form
            ,load = null
            ,active = {
                reload: function () {
                    var game_id = $('#game_id').val();

                    table.reload('tableId', {
                        page: {curr: 1}
                        ,where: {game_id:game_id}
                        ,height: 'full-100'
                    })
                },
            };

        //表格初始化
        table.render({
            elem: '#tableBox'
            ,height: 'full-100'
            ,url: '{:url("RebateRule/index")}'
            ,page: true
            ,limit:20
            ,limits:[20,40,60,80]
            ,loading:true
            ,id:'tableId'
            ,done: function () {
                layer.close(load);
                $('#layui-loading').hide();
            }
            ,cols: [[
                {field:'id', title: 'ID', width: 100, sort:true}
                ,{field:'type', title: '类型', width: 150,}
                ,{field:'rule', title: '返利规则', width: 200,}
                ,{field:'name', title: '返利名称', width: 200}
                ,{field:'starttime', title: '开始时间', width: 150,}
                ,{field:'endtime', title: '结束时间', width: 150}
                ,{field:'priority', title: '优先级', width: 150, edit: 'text'}
                ,{field:'addtime', title: '添加时间', width: 150}
                ,{title: '操作', align:'center', toolbar:'#tableBar'}
            ]]
        });

        //监听搜索栏
        $('#add').on('click', function(){
            layer.open({
                type: 2
                ,title: '添加'
                ,area: ['100%', '100%']
                ,content: '{:url("RebateRule/add")}'
            });
        })

        //监听搜索下拉菜单
        form.on('select(game_id)', function(data){
            load = layer.load();
            active['reload'].call(this)
        });

        //监听工具条
        table.on('tool(tableBox)', function(obj){
            var data = obj.data;
            if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '编辑'
                    ,resize: true
                    ,area: ['100%', '100%']
                    ,content: '{:url("RebateRule/edit")}?id='+data.id
                    ,cancel: function(){
                        $.post('{:url("RebateRule/index")}',{id: data.id}, function(json){
                            obj.update({
                                type: json.data[0].type
                                ,rule: json.data[0].rule
                                ,name: json.data[0].name
                                ,starttime: json.data[0].starttime
                                ,endtime: json.data[0].endtime
                            })
                        }, 'JSON').error(function(){
                            layer.close(load);
                            layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                        });
                    }
                });
            }else if(obj.event === 'del'){
                layer.confirm('确定要删除吗？', {offset: '250px'}, function(index){
                    var load = layer.load();
                    $.post('{:url("RebateRule/del")}',{id: data.id}, function(json){
                        layer.close(load);
                        if(json.code==0){
                            obj.del();
                            layer.msg(json.msg, {icon: 1, time:1000})
                        }else{
                            layer.msg(json.msg, {icon: 0, time:1000});
                        }
                    }, 'JSON').error(function(){
                        layer.close(load);
                        layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                    });
                });
            }
        });

        //监听单元格编辑
        table.on('edit(tableBox)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field
                ,params = {}
                ,load = layer.load();

            if(field=='priority'){
                params = {id:data.id, priority:value};
            }
            $.post('{:url("RebateRule/edit")}',params, function(json){
                layer.close(load);
                if(json.code==0){
                    layer.msg(json.msg, {icon: 1, time:1000})
                }else{
                    layer.msg(json.msg, {icon: 0, time:1000});
                }
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
        });
    });
</script>
{/block}